﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.Serialization;
using System.Xml.Serialization;

namespace Collab.Services.Remote.Entities
{
    [DataContract(Namespace = "http://collab.codeplex.com/2010/10/Schemas")]
    public class BufferUpdate : IExtensibleDataObject
    {
        [XmlAttribute]
        [DataMember(Order = 1, IsRequired = true)]
        public string DocumentName { get; set; }

        [XmlAttribute]
        [DataMember(Order = 2, IsRequired = true)]
        public Guid Sender { get; set; }

        [XmlAttribute]
        [DataMember(Order = 3, IsRequired = true)]
        public int Revision { get; set; }

        [XmlAttribute]
        [DataMember(Order = 4, IsRequired = true)]
        public int UpdateIndex { get; set; }

        [DataMember(Order = 5, IsRequired = true)]
        public List<Change> Changes { get; set; }

        /// <summary>
        /// Do not use!
        /// </summary>
        public BufferUpdate()
        {
            throw new InvalidOperationException("Only used for serialization");
        }

        public BufferUpdate(string documentName, Guid sender, int revision, List<Change> changes)
         {
            Debug.Assert(!string.IsNullOrWhiteSpace(documentName));
            Debug.Assert(revision > 0);
            Debug.Assert(sender != null);
            Debug.Assert(changes != null);

            this.DocumentName = documentName;
            this.Revision = revision;
            this.Sender = sender;
            this.Changes = changes;
        }

        public BufferUpdate(string documentName, Guid sender, int revision, int updateIndex, List<Change> changes) : this(documentName, sender, revision, changes)
        {
            this.UpdateIndex = updateIndex;
        }

        #region IExtensibleDataObject Members

        public ExtensionDataObject ExtensionData { get; set; }

        #endregion
    }
}
